package com.yami.shop.api.controller;

import com.yami.shop.bean.model.*;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.CashOutService;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取小程序端,推广页面的各项数据
 */
@RestController
@RequestMapping("/Spread")
public class SpreadController {
    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private UserService userService;


    /**
     * 获取推广首页的数据
     */
    @GetMapping("/getpromotionIncome")
    public ResponseEntity<Map> getpromotionIncome(String id){
        Map map = new HashMap();
        String userId = SecurityUtils.getUser().getUserId();
        //获取累计推广金额
        String allIncome = cashOutService.getAllIncome(userId);
        //获取可提现金额
        User user = userService.getUserByUserId(userId);
        Double account = 0.00;
        if(null!=user){
            account =  user.getAccount();
        }
        map.put("allIncome",allIncome);
        map.put("account",account);
        return ResponseEntity.ok(map);
    }

    /**
     * 累计邀请
     */
    @GetMapping("/getCumulativeInvitation")
    public ResponseEntity<Map> getCumulativeInvitation(){
        Map map = new HashMap();
        String userId = SecurityUtils.getUser().getUserId();
        //查询被邀请的人的集合
        List<User> userList = userService.getUserByPuserid(userId);
        List newList = new ArrayList();
        if(null!=userList){
            for(User user:userList){
                CumulativeInvitation cumulativeInvitation = new CumulativeInvitation();
                String userid = user.getUserId();
                String pic = user.getPic();
                //获取累计订单数
                String ordernumber = userService.getOrderNumber(userid,userId);
                //获取累计返利金额
                String allRebateMoney = cashOutService.getMoneyByCode(userid,userId);
                cumulativeInvitation.setName(user.getNickName());
                cumulativeInvitation.setPic(pic);
                cumulativeInvitation.setNumber(ordernumber);
                cumulativeInvitation.setRebateMoney(allRebateMoney);
                newList.add(cumulativeInvitation);
            }
        }
        map.put("spreadList",newList);
        map.put("friendSize",userList.size());
        return ResponseEntity.ok(map);
    }

    /**
     * 获得推广订单
     */
    @GetMapping("/getPromoTionOrder")
    public ResponseEntity<Map>  getPromoTionOrder(){
        Map map = new HashMap();
        String userId = SecurityUtils.getUser().getUserId();
        //获得全部推广订单
        List<PromoTionOrder> promoTionOrderList = cashOutService.getPromoTionOrder(userId);
        if(null!=promoTionOrderList){
            for(PromoTionOrder promoTionOrder:promoTionOrderList){
                //订单编号
                String orderNumber = promoTionOrder.getOrderNumber();
               //获取子订单
                List<OrderItem> orderItemList = cashOutService.getOrderItem(orderNumber);
                promoTionOrder.setOrderItemList(orderItemList);
            }
            map.put("AllpromoTionOrder",promoTionOrderList);
            map.put("AllpromoTionOrderSize",promoTionOrderList.size());
        }
        //获得全部待结算的订单
        List<PromoTionOrder> dsjOrderList = cashOutService.getdjsOrder(userId);
        if(null!=dsjOrderList){
            for(PromoTionOrder promoTionOrder:dsjOrderList){
                //订单编号
                String orderNumber = promoTionOrder.getOrderNumber();
                //获取子订单
                List<OrderItem> orderItemList = cashOutService.getOrderItem(orderNumber);
                promoTionOrder.setOrderItemList(orderItemList);
            }
            map.put("djsOrderList",dsjOrderList);
        }
        //获得所有结算的订单
        List<PromoTionOrder> yjsOrderList = cashOutService.getyjsOrder(userId);
        if(null!=yjsOrderList){
            for(PromoTionOrder promoTionOrder:yjsOrderList){
                //订单编号
                String orderNumber = promoTionOrder.getOrderNumber();
                //获取子订单
                List<OrderItem> orderItemList = cashOutService.getOrderItem(orderNumber);
                promoTionOrder.setOrderItemList(orderItemList);
            }
            map.put("yjsOrderList",yjsOrderList);
            map.put("yjsOrderListSize",yjsOrderList.size());
        }
        return  ResponseEntity.ok(map);
    }

    /**
     * 收支明细
     */
    @GetMapping("/getIncomeDetails")
    public ResponseEntity<Map> getIncomeDetails(){
        Map map = new HashMap();
        String userId = SecurityUtils.getUser().getUserId();
        //获取当前用户已结算的订单佣金
        List<RebateRecords> rebateRecordsList = cashOutService.getIncomeDetails(userId);
        map.put("rebateRecordsList",rebateRecordsList);
        return ResponseEntity.ok(map);
    }

    /**
     * 推广商品
     */
    @GetMapping("/getPromotionGoods")
    public ResponseEntity<Map> getPromotionGoods(String shopId){
        Map map = new HashMap();
        //获取所有的推广商品
        List<PromotionGoods> promotionGoodsList = cashOutService.getPromotionGoods(shopId);
        map.put("promotionGoodsList",promotionGoodsList);
        //获取最新商品
        List<PromotionGoods> recentGoodsList = cashOutService.getRecentGoods(shopId);
        map.put("recentGoodsList",recentGoodsList);
        //获取最热推广商品
        List<PromotionGoods> sellWellGoodsList = cashOutService.getSellWellGoods(shopId);
        map.put("sellWellGoodsList",sellWellGoodsList);
        return  ResponseEntity.ok(map);
    }

    /**
     * 提现记录
     */
    @GetMapping("/getWithdrawalRecord")
    public ResponseEntity<Map> getWithdrawalRecord(){
        Map map = new HashMap();
        String userId = SecurityUtils.getUser().getUserId();
        //获取全部个人提现记录
        List<CashOut> cashOutList = cashOutService.getWithdrawalRecord(userId);
        //获取已完成的提现记录
        List<CashOut> ytgCashOutList = cashOutService.getytgWithdrawalRecord(userId);
        //获取待审核的提现记录
        List<CashOut> dshCashOutList = cashOutService.getdshWithdrawalRecord(userId);
        map.put("cashOutList",cashOutList);
        map.put("ytgCashOutList",ytgCashOutList);
        map.put("dshCashOutList",dshCashOutList);
        return  ResponseEntity.ok(map);
    }


}
